<?php
/*
  功能：文章 model
  作者：cxm
  时间：2019-10-23
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Article extends BaseModel {
    use SoftDeletes;
    protected $dates = ['deleted_at']; //开启deleted_at
    protected $table='article';  //绑定表

    /**
     * 图片
     */
    public function file(){
        return $this->hasOne(File::class,'id','file_id');
    }

    /**
     * 判断分类是否已添加文章
     * @param array $classifyIds
     * @return boolean
     */
    public static function getNum($classifyIds=array()){
        if (!empty($classifyIds)) {
            foreach ($classifyIds as $item) {
                if (self::newsNum($item) > 0) {
                    return true;
                    break;
                }
            }
        }
        return false;
    }

    /**
     * 查询分类下的文章数量
     * @param $classify_id
     * @return number
     */
    public static function newsNum($classify_id){
        return self::where('class_id',$classify_id)->count();
    }
}